# -*- coding: utf-8 -*-

from PyQt5.QtWidgets import *
import sys
from PyQt5.Qt import *


class InputDia(QWidget):
    def __init__(self):
        super(InputDia, self).__init__()
        self.setWindowTitle("QInputDialog 示例")
        layout = QFormLayout()

        self.btn_list = QPushButton("获取列表选项")
        self.btn_list.clicked.connect(self.get_list)
        self.edit = QLineEdit()
        layout.addRow(self.btn_list, self.edit)

        self.btn_str = QPushButton("获取字符串")
        self.btn_str.clicked.connect(self.get_str)
        self.edit1 = QLineEdit()
        layout.addRow(self.btn_str, self.edit1)

        self.btn_int = QPushButton("获取整数")
        self.btn_int.clicked.connect(self.get_int)
        self.edit2 = QLineEdit()
        layout.addRow(self.btn_int, self.edit2)

        self.setLayout(layout)

    def get_list(self):
        list_ = ["C", "C++", "Python"]
        # 固定格式，将可选数据写入
        # 获取item输入的值，以及ok键的点击与否（True 或False）
        # QInputDialog.getItem(self,标题,文本,元组,元组默认index,是否允许更改)
        item, ok = QInputDialog.getItem(self, "select input dialog", "语言列表", list_, 0, False)
        if ok and item:
            self.edit.setText(item)

    def get_str(self):
        item, ok = QInputDialog.getText(self, "Text Input", "输入字符串")
        if ok and item:
            self.edit1.setText(str(item))

    def get_int(self):
        item, ok = QInputDialog.getInt(self, "Int input", "输入整数")
        if ok and item:
            self.edit2.setText(str(item))


if __name__ == '__main__':
    app = QApplication(sys.argv)
    dia = InputDia()
    dia.show()
    sys.exit(app.exec_())
